package springboot.kafka.util;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 日期工具类：为了保证SimpleDateFormath和Calendar的线程安全
 */
public class DateUtils {

    private static ThreadLocal<DateFormat> sdfThreadLocal = new ThreadLocal<DateFormat>() {
        @Override
        public SimpleDateFormat initialValue() {
            return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        }
    };

    public static Date parse(String strDate) {
        try {
            return sdfThreadLocal.get().parse(strDate);
        } catch (Exception e) {
            return null;
        }
    }

    public static String formatDate(Date date) {
        try {
            return sdfThreadLocal.get().format(date);
        } catch (Exception e) {
            return null;
        }
    }

}
